<?php
/**
 * Ginre is stand for Ginre is not regular express,
 * And its aim is having the same function to regular express but with more faster deal
 * speed for many simple work to deal with the strings.
 * But Ginre is can't full instead of Regular Express. Because it have more power
 * to deal with compact thing.
 *
 * @author gjunming@gmail.com
 * @version 0.2
 * @since 2009-09-17
 */
class Ginre{
    /**
     * @param <type> $poseString1
     * @param <type> $poseString2
     * @return <type>
     * @since version 0.1
     */
    function getBetween($string, $poseString1, $poseString2){
        $tmp = explode($poseString1, $string);
        $tmp = explode($poseString2, $tmp[1]);
        return trim($tmp[0]);
    }
    /**
     * @param <string> $string
     * @param <string> $ends
     * @return <boolean>
     * @assert("hello world","ld")==true
     * @assert("hello world","dd")==false
     * @since version 0.2
     */
    function isEndWith($string, $ends){
        return (substr($string, strlen($ends) * -1) === $ends) ? TRUE : FALSE;
    }
    function lastPart($string, $delimiter){
        return array_pop(explode($delimiter, $string));
    }
    function firstPart($string, $delimiter){
        return array_shift(explode($delimiter, $string));
    }
    function getPart($string, $delimiter, $index){
        $sepratedParts = explode($delimiter, $string);
        if($index < 0){
            $index = count($sepratedParts) + $index;
        }
        return $sepratedParts[$index];
    }
    function rmLastPart($string, $delimiter){
        $parts = explode($delimiter, $string);
        return substr($string, 0, strlen($string) - strlen($parts[count($parts) - 1]) - strlen($delimiter));
    }
    function rmFirstPart($string, $delimiter){
        $parts = explode($delimiter, $string);
        return substr($string, -1 * (strlen($string) - strlen($parts[0]) - strlen($delimiter)));
    }
}
function lastPart($string, $delimiter){
    return array_pop(explode($delimiter, $string));
}
function lastPart2($string, $delimiter){
    $parts = explode($delimiter, $string);
    return substr($string, -1 * strlen($parts[count($parts) - 1]));
}
function testPart($fncName, $string, $delimiter){
    $start = time();
//    echo "start at $start\n";
    for($index = 0; $index < 100000; $index++){
        $fncName($string, $delimiter)."$index\n";
    }
    $end = time();
//    echo "end at $end\n";
    return $end - $start;
}
$r1 = $r2 = 0;
for($index = 0; $index < 100; $index++){
    $r1 += testPart("lastPart", "helo_hello_hello_hello_hello", "_");
    $r2 += testPart("lastPart2", "helo_hello_hello_hello_hello", "_");
}
echo "part1 - $r1";//132
echo "\n part2 - $r2";//161
?>
